package com.qiyuesuo.controller;

import com.qiyuesuo.entity.Person;
import com.qiyuesuo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Value("${server.port}")
    public Integer port;

    @GetMapping("/test")
    public String get() {
        return port.toString();
    }

    @PostMapping("/registry")
    public String registry(@RequestBody Person person) {
        try {
            userService.registry(person);
            return "SUCCESS";
        } catch (Exception e) {
            logger.error("registry error {}", e.getMessage());
        }
        return "ERROR";
    }

    @PostMapping("/login")
    public String login(@RequestBody Person person) {
        try {
            return userService.login(person);
        } catch (Exception e) {
            logger.error("login error {}", e.getMessage());
        }
        return "ERROR";
    }
}
